package com.geek.javase.ch17.work.tcpwork.tcp4;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/*
 *@Date 2025/11/4
 *@Author wangpeng
 *@Description
 * 读写撤离
 */
public class WriteThread implements Runnable {
    private Socket socket;
    public WriteThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try(
               Scanner sc = new Scanner(System.in);
               PrintWriter pw = new PrintWriter(socket.getOutputStream());
        ){
            while(true){
                System.out.println("请说：");
                String line = sc.nextLine();
                pw.println(line);
                pw.flush();

                if(line.equals("exit")){
                    break;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
        }
    }
}
